home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / thunderbirdCompose.js < prev    next >
Text File  |  2010-02-02  |  4KB  |  128 lines

  1. /* Copyright (c) 2006 YourNameHere
  2.    See the file LICENSE.txt for licensing information.
  3.    */
  4. var WisestampCompose =
  5. {
  6.     _prefs: null,
  7.     app: null,
  8.     currentType: "",
  9.     open: false,
  10.     sessionID: null,    
  11.     get prefs()
  12.     {
  13.         if (!this._prefs)
  14.         {
  15.             this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
  16.             getService(Components.interfaces.nsIPrefService);
  17.             this._prefs = this._prefs.getBranch("extensions.wisestamp.");
  18.         }
  19.         return this._prefs;
  20.     },
  21.     
  22.     updateEditorHtml: function(editor,html)
  23.     {
  24.         editor.rootElement.innerHTML = html;
  25.     },
  26.     
  27.     insertSignature: function(type)
  28.     {
  29.         try
  30.         {
  31.             var editor = GetCurrentEditor();
  32.             if (editor instanceof Components.interfaces.nsIHTMLEditor)
  33.             {
  34.                 var body = editor.document.documentElement;
  35.                 var html = body.innerHTML;
  36.                     
  37.                 var prefix = WisestampSignatureFactory.PREFIX.replace("SIG_","SIG_"+this.sessionID+"_");
  38.                 var suffix = WisestampSignatureFactory.SUFFIX.replace("SIG_","SIG_"+this.sessionID+"_");
  39.                 var sig = prefix + WisestampSignatureFactory.createSignature(null, type) + suffix;
  40.                 var newBody = null;
  41.                 
  42.                 var pattern = new RegExp(prefix.replace("!", "\!") + "(.|\n)*" + suffix.replace("!", "\!"));
  43.                 if (html.match(pattern))
  44.                     newBody = html.replace(pattern, sig);
  45.                 else
  46.                     newBody = html.replace(/(\<body.*?\>)/, "$1"+"<br/><br/>" + sig);
  47.                 
  48.                 this.updateEditorHtml(editor,newBody);
  49.             }
  50.             
  51.         } catch(e)
  52.         {
  53.             WiseStampUtils.log("[thunderbirdCompose.js::insertSignature] exception caught: " + e);
  54.         }
  55.     },
  56.     
  57.     onUseSignature: function (event, type, popup)
  58.     {
  59.         this.insertSignature(type);
  60.     },
  61.     
  62.     onCompose: function (event, type, popup)
  63.     {
  64.         WiseStampUtils.log("[thunderbirdCompose.js::onCompose] >>>>> ");
  65.         
  66.         var autoInsertOnCompose = WiseStampPrefs.getBoolPref("autoinsert_compose",true,"autoinsert");
  67.         var autoInsertOnThread = WiseStampPrefs.getBoolPref("autoinsert_thread",true,"autoinsert");
  68.  
  69.         var newMessage = (gMsgCompose.compFields.subject == "");
  70.         if ((newMessage && !autoInsertOnCompose) || (!newMessage && !autoInsertOnThread)) 
  71.             return;
  72.         
  73.         this.insertSignature(type);                
  74.     },
  75.     
  76.     onLoad: function ()
  77.     {
  78.         WiseStampUtils.log("[thunderbirdCompose.js::onLoad] >>>>> ");
  79.         
  80.         this.sessionID = WiseStampUtils.getRandomInt(10000,99999);
  81.         
  82.         this.currentType = WisestampCompose.prefs.getCharPref("type");
  83.         WisestampOverlay.app = WisestampGetAppObject();
  84.         if (!this.prefs.getBoolPref("enabled"))
  85.             document.getElementById("wisestampButton").setAttribute("disabled", "true");
  86.         
  87.         var hide = this.prefs.getBoolPref("hidebuttons");
  88.         var bttn = document.getElementById("wisestampButton");
  89.         bttn.setAttribute("hidden", hide);
  90.         this.open = true;
  91.         
  92.         //WisestampCompose.onUseSignature(null,this.currentType);
  93.         setTimeout(function(){WisestampCompose.onCompose(null,this.currentType);},500,null);
  94.     },
  95.     
  96.     onFocus: function ()
  97.     {
  98.         if (!this.open)
  99.         {
  100.             this.onLoad();
  101.         }
  102.     },
  103.     
  104.     onClose: function ()
  105.     {
  106.         this.open = false;
  107.     },
  108.     
  109.     unLoad: function ()
  110.     {
  111.         //if (gMsgCompose)
  112.         //  gMsgCompose.UnregisterStateListener(WisestampStateListener);
  113.     }
  114. }
  115.  
  116. function WisestampGetAppObject()
  117. {
  118.     var app = new WisestampThunderbird();
  119.     app.onLoad = function (){};
  120.     return app;
  121. }
  122.  
  123. window.addEventListener("compose-window-reopen",function(e){WisestampCompose.onLoad()},true);
  124. window.addEventListener("load",function(e){WisestampCompose.onLoad()},true);
  125. window.addEventListener("close",function(){WisestampCompose.onClose()},true);
  126. window.addEventListener("unload",function(){WisestampCompose.unLoad()},false);
  127. document.documentElement.addEventListener("focus",function(){WisestampCompose.onFocus()},true);
  128.